package cn.linlane.controller.sys;

import cn.linlane.config.Realm;
import cn.linlane.controller.BaseController;
import cn.linlane.entity.errors.MyError;
import cn.linlane.entity.exception.MyException;
import cn.linlane.entity.sys.SysDepart;
import cn.linlane.entity.sys.SysUser;
import cn.linlane.service.sys.ShiroService;
import cn.linlane.service.sys.SysDepartService;
import cn.linlane.service.sys.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

@Api(value = "组织机构接口说明", description = "组织机构接口说明")
@RestController
@RequestMapping(value = "/organization")
public class SysDepartController extends BaseController {
    @Autowired
    private SysDepartService sysDepartService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private ShiroService shiroService;

    @ApiOperation(value = "查询当前用户门店数据权限", httpMethod = "GET", response = SysDepart.class)
    @GetMapping("/departtree")
    public Object departtree() {
         return setSuccessMyMap(sysDepartService.getDepartTree());
    }

    @ApiOperation(value = "查询所有门店的树", httpMethod = "GET", response = SysDepart.class)
    @GetMapping("/departall")
    public Object departall() {
        return setSuccessMyMap(sysDepartService.getAllDepart());
    }

    @ApiOperation(value = "根据编号查询状态", httpMethod = "POST", response = SysDepart.class)
    @PostMapping("/status/{storeCode}")
    public Object getfindByDepartCode(@RequestParam String storeCode){
        return setSuccessMyMap(sysDepartService.findByDepartCode(storeCode));
    }

    @ApiOperation(value = "根据部门id或部门名称进行查询", httpMethod = "POST", response = SysDepart.class)
    @PostMapping("/departs")
    public Object getDeparts(@ApiParam(value = "部门Id。（必填）") @RequestParam Integer id,
                             @ApiParam(value = "部门编号或名称") @RequestParam(required = false, defaultValue = "") String searchKey,
                             @ApiParam(value = "当前页数") @RequestParam(required = false, defaultValue = "0") Integer pageNum,
                             @ApiParam(value = "分页数量") @RequestParam(required = false, defaultValue = "30") Integer pageSize){
        return setSuccessMyMap(sysDepartService.getDeparts(id, searchKey, pageNum, pageSize));
    }

    @ApiOperation(value = "添加机构信息", httpMethod = "POST", response = SysDepart.class)
    @PostMapping
    public Object post(@RequestBody SysDepart sysDepart){
        Assert.notNull(sysDepart.getStoreCode(), "部门编码不能为空！");
        Assert.notNull(sysDepart.getStoreName(), "部门名称不能为空");

        if ( !sysDepartService.existsByStoreCode(sysDepart.getStoreCode() )){
            SysUser sysUser = shiroService.getSysUser();
            sysDepartService.updateDataPermition(sysUser.getId(), sysDepart.getStoreCode());
            return setSuccessMyMap(sysDepartService.add(sysDepart));
        } else {
            throw new MyException(MyError.DEPART_EXISTS);
        }
    }

    @ApiOperation(value = "修改机构信息", httpMethod = "PUT", response = SysDepart.class)
    @PutMapping
    public Object put(@RequestBody SysDepart sysDepart) {
        Assert.notNull(sysDepart.getStoreCode(), "部门编码不能为空！");
        Assert.notNull(sysDepart.getStoreName(), "部门名称不能为空");

        if ( sysDepartService.existsByStoreCode(sysDepart.getStoreCode() )){
            return setSuccessMyMap(sysDepartService.update(sysDepart));
        } else {
            throw new MyException(MyError.DEPART_NOT_EXISTS);
        }
    }

    @ApiOperation(value = "更改机构启禁用状态", httpMethod = "PUT", response = SysDepart.class)
    @PutMapping("/status")
    public Object putState(@RequestParam Integer id, @RequestParam boolean isEnabled) {
        SysDepart sysDepart = sysDepartService.queryById(id);
        sysDepart.setEnabled(isEnabled);
        return setSuccessMyMap(sysDepartService.update(sysDepart));
    }

    @ApiOperation(value = "删除机构", httpMethod = "DELETE", response = void.class)
    @DeleteMapping("/{id}")
    public Object delete(@PathVariable Long id) {
        //从数据权限中删掉所有记录
        sysDepartService.deleteDataPermition(id);
        sysDepartService.delete(id);
        return setSuccessMyMap(null);
    }

}
